package com.tarena.lbs.business.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.business.dao.mapper.BusinessMapper;
import com.tarena.lbs.business.dao.repository.BusinessRepository;
import com.tarena.lbs.business.pojo.po.BusinessPO;
import com.tarena.lbs.business.pojo.query.BusinessQuery;
import com.tarena.lbs.business.pojo.vo.BusinessStoreVO;
import com.tarena.lbs.business.pojo.vo.BusinessVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Repository
public class BusinessRepositoryImpl implements BusinessRepository {
    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public PageInfo<BusinessVO> selectList(BusinessQuery query) {
        QueryWrapper<BusinessPO> queryWrapper = new QueryWrapper();
//        queryWrapper.eq("business_status",query.getBusinessStatus());
        if (query.getBusinessStatus() != null) {
            queryWrapper.eq("business_status", query.getBusinessStatus());
        }
        if (query.getBusinessName() != null && !query.getBusinessName().isEmpty()) {
            queryWrapper.like("business_name", query.getBusinessName()); // 模糊匹配商店名称
        }
        if (query.getBusinessHeadPhone()!= null && !query.getBusinessHeadPhone().isEmpty()) {
            queryWrapper.eq("business_head_phone", query.getBusinessHeadPhone()); // 负责人手机号
        }
        if (query.getStartingTime() != null) {
            queryWrapper.ge("apply_date", query.getStartingTime()); // 大于等于开始日期
        }
        if (query.getEndTime() != null) {
            queryWrapper.le("apply_date", query.getEndTime()); // 小于等于结束日期
        }
        //开启分页插件
        PageHelper.startPage(query.getPageNo(),query.getPageSize());

        List<BusinessPO> businessPOS = businessMapper.selectList(queryWrapper);
        List<BusinessVO> businessVOS = businessPOS.stream().map(businessPO -> {
            BusinessVO businessVO = new BusinessVO();
            BeanUtils.copyProperties(businessPO,businessVO);
            return businessVO;
        }).collect(Collectors.toList());

        return new PageInfo<>(businessVOS);
    }

    @Override
    public void insert(BusinessPO businessPO) {
        businessMapper.insert(businessPO);
    }

    @Override
    public BusinessStoreVO detail(Integer businessId) {
        BusinessPO businessPO = businessMapper.selectById(businessId);
        BusinessStoreVO businessStoreVO = new BusinessStoreVO();
        if (businessPO != null) {
            BeanUtils.copyProperties(businessPO,businessStoreVO);
        }
        return businessStoreVO;
    }

}
